﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.Controls
{
    public partial class AddContractCtrl : BaseListControl, IListControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
        }

        public String NetworksSessionID
        {
            get
            {
                if (ViewState[ClientID + "_NetworksSessionID"] == null)
                    ViewState[ClientID + "_NetworksSessionID"] = Guid.NewGuid().ToString() + "_" + ClientID + "_NetworksSessionID";
                return ViewState[ClientID + "_NetworksSessionID"].ToString();
            }
            set { ViewState[ClientID + "_NetworksSessionID"] = value; }
        }

        public Networks NetworkList
        {
            get { return (Networks)Session[NetworksSessionID]; }
            set { Session[NetworksSessionID] = value; }
        }

        public Contracts NewContracts
        {
            get { return (Contracts)SourceObject; }
            set { SourceObject = value; }
        }

        public int DumpID
        {
            get
            {
                if (ViewState[ClientID + "_DumpID"] == null || !(ViewState[ClientID + "_DumpID"] is int))
                    ViewState[ClientID + "_DumpID"] = 0;
                return (int)ViewState[ClientID + "_DumpID"];
            }
            set { ViewState[ClientID + "_DumpID"] = value; }
        }

        public void AddIMEI(string imei)
        {
            String oldFilter = "";
            try
            {
                View_ProductDetails prodDetails = View_ProductDetails.GetAvailableProductDetailsByIMEI(imei);
                if (prodDetails == null || prodDetails.RowCount <= 0)
                {
                    throw new Exception( String.Format("(*) IMEI {0} is not available!", imei));
                }

                if (NewContracts == null)
                    NewContracts = new Contracts();

                oldFilter = NewContracts.Filter;
                NewContracts.Filter = Contracts.ColumnNames.ProductDetailID + "=" + prodDetails.ID;
                if (NewContracts.RowCount > 0)
                {
                    NewContracts.Filter = oldFilter;
                    ShowErrorMessage(grid, String.Format("(*) IMEI {0} has already been added. Please choose another one.", imei));
                    if (NewContracts.IsColumnNull("ID"))
                        NewContracts.MarkAsDeleted();
                    return;
                }
                else
                {
                    NewContracts.Filter = oldFilter;
                    NewContracts.AddNew();
                }

                
                if (prodDetails.RowCount > 0)
                {
                    NewContracts.ID = --DumpID;
                    NewContracts.Deleted = false;
                    NewContracts.ProductDetailID = prodDetails.ID;
                    NewContracts.Network = "";
                    NewContracts.Cost = prodDetails.Cost;
                    NewContracts.Product = prodDetails.ProductName + "<br/><span class='hintBold'>Serial/IMEI:" + imei + "</span>";
                }

                NewContracts.Filter = oldFilter;
                Refresh();

            }
            catch (Exception ex)
            {
                try
                {
                    if (NewContracts != null && NewContracts.IsColumnNull("ID"))
                    {
                        NewContracts.MarkAsDeleted();
                        NewContracts.Filter = oldFilter;
                    }
                }
                catch (Exception ex1)
                {
                }

                ErrorLog.WriteToFile("AddContractCtrl.ascx.cs", "AddIMEI", ex.Message);
                throw new Exception(ex.Message);
            }
        }

        #region IListControl Members

        public void DeleteCheckedItems()
        {
            String checkedItemID = "";
            for (int i = 0; i < grid.Items.Count; i++)
            {
                DataGridItem item = grid.Items[i];
                Control ctrl = item.FindControl("cbSelect");
                if (ctrl != null && ctrl is CheckBox && ((CheckBox)ctrl).Checked && ((CheckBox)ctrl).Attributes["ItemID"] != null)
                    checkedItemID += ((CheckBox)ctrl).Attributes["ItemID"].ToString() + ",";
            }
            if (checkedItemID != "")
                checkedItemID = checkedItemID.Remove(checkedItemID.Length - 1);
            checkedItemID = "(" + checkedItemID + ")";

            String oldFilter = Filter;
            if (!String.IsNullOrEmpty(oldFilter))
                Filter = Filter + " and (" + Contracts.ColumnNames.ID + " in " + checkedItemID + ")";
            else
                Filter = Contracts.ColumnNames.ID + " in " + checkedItemID;

            NewContracts.Rewind();
            while (NewContracts.RowCount > 0)
            {
                NewContracts.MarkAsDeleted();
                NewContracts.MoveNext();
            }

            //if (!SaveOnCommit)
            //    NewContracts.Save();

            Filter = oldFilter;
        }

        public void DeleteSelectedItem()
        {
            throw new NotImplementedException();
        }

        public void AddNewItem(MyGeneration.dOOdads.SqlClientEntity newItem)
        {
            throw new NotImplementedException();
        }

        public void UpdateItem(MyGeneration.dOOdads.SqlClientEntity updatedItem)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}